﻿#include <gtest/gtest.h>

#if __cplusplus < 202002L
#error need c++20 support, detail in readme
#endif

#include "xml.h"
#include "svgreader.h"


static bool operator==(const SvgCanvasInfo& lhs, const SvgCanvasInfo& rhs)
{
	if (lhs.width == rhs.width
		&& lhs.height == rhs.height
		&& lhs.background == rhs.background
		&& lhs.shapes.size() == rhs.shapes.size())
	{
		// 因为子类的原因，这里实现比较麻烦，所以放弃, 下面直接比较图形
		return true;
	}
	return false;
}

class SvgReaderTest : public ::testing::Test
{
protected:
	void SetUp() override
	{
		svg = { {"svg", {{"subset", "wps"}, {"height", "500"}, {"width", "500"}}, {
			{"rect", {{"x", "0"}, {"y", "0"}, {"height", "500"}, {"width", "500"},
				{"stroke","rgb(255, 255, 255, 1)"}, {"stroke-width","2"}, {"fill","rgb(255, 255, 255, 1)"}}, {}},
			}} };

		canvasInfo.height = 500;
		canvasInfo.width = 500;
		canvasInfo.background = QColor(255, 255, 255, 255);

		lineEle = { "line", {{"x1", "-4.5"}, {"y1", "72.5"} , {"x2", "188.5"}, {"y2", "185.5"} , {"stroke", "rgb(0, 0, 0, 1)"}, { "stroke-width", "2"}}, {} };
		line = std::make_unique<SvgLine>(QLineF{ {-4.5, 72.5}, {188.5, 185.5} });

		rectEle = { "rect", {{"x", "161.5"}, { "y", "97.5"}, { "width", "94"}, { "height", "94" }, {"stroke", "rgb(151, 148, 75, 1)"},
			{"stroke-width", "2"}, {"fill", "rgb(211, 190, 231, 1)"}}, {} };
		rect = std::make_unique<SvgRect>(QRectF{ QPointF{161.5, 97.5}, QSizeF{94, 94} });
		rect->Pen().setColor(QColor{151, 148, 75, 255});
		rect->Brush().setColor(QColor{211, 190, 231, 255});
	}

	xml::Elements svg;
	SvgCanvasInfo canvasInfo;

	xml::Element lineEle;
	unique_ptr<SvgLine> line;

	xml::Element rectEle;
	unique_ptr<SvgRect> rect;
};

TEST_F(SvgReaderTest, empty_svg)
{
	EXPECT_EQ(ReadSvg(svg), canvasInfo);
}

TEST_F(SvgReaderTest, one_line_svg)
{
	svg[0].subs.push_back(lineEle);

	const auto res = ReadSvg(svg);

	EXPECT_EQ(dynamic_cast<SvgLine&>(*(res.shapes[0])), *line);
}

TEST_F(SvgReaderTest, one_rect_svg)
{
	svg[0].subs.push_back(rectEle);

	const auto res = ReadSvg(svg);

	EXPECT_EQ(dynamic_cast<SvgRect&>(*(res.shapes[0])), *rect);
}